<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <!-- <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="sendid"
          label="发送人"
        >
          <a-select
            showSearch
            v-model="model.sendid"
            placeholder="请选择发送人(可搜索)"
            @search="searchChange($event)"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option
              :value="item.id"
              v-for="(item, index) in userlist"
              :key="index"
              >{{ item.realname }}</a-select-option
            >
          </a-select>
        </a-form-model-item> -->
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="receiverid"
          label="接收人"
        >
          <a-select
            showSearch
            v-model="model.receiverid"
            placeholder="请选择接收人(可搜索)"
            @search="searchChange($event)"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option
              :value="item.id"
              v-for="(item, index) in userlist"
              :key="index"
              >{{ item.realname }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="pushType"
          label="推送类型"
        >
          <!-- <a-select v-model="model.pushType" placeholder="请选择推送类型">
            <a-select-option value="咨询消息">咨询消息</a-select-option>
            <a-select-option value="系统消息">系统消息</a-select-option>
          </a-select> -->
          <a-radio-group v-model="model.pushType">
            <a-radio value="咨询消息">
              咨询消息
            </a-radio>
            <a-radio value="系统消息">
              系统消息
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="title"
          label="标题"
        >
          <a-input placeholder="请输入标题" v-model="model.title" />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="content"
          label="内容"
        >
          <a-textarea placeholder="请输入内容" v-model="model.content" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from "@/api/manage";
import { sysUserList } from "@/api/tsapi";
import moment from "moment";

export default {
  name: "MessageNoticeModal",
  data() {
    return {
      title: "操作",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        sendid: [{ required: true, message: "请选择发送人" }],
        receiverid: [{ required: true, message: "请选择接收人" }],
        title: [{ required: true, message: "请输入标题" }],
        content: [{ required: true, message: "请输入内容" }],
        pushType: [{ required: true, message: "请选择推送类型" }],
      },
      url: {
        add: "/MessageNotice/messageNotice/add",
        edit: "/MessageNotice/messageNotice/edit",
      },
      userlist: [], //接收接收人的数据
      sendid: localStorage.getItem("userid"),
    };
  },
  created() {
    this.syslist();
  },
  methods: {
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    syslist(e) {
      let params = {
        realname: e,
      };
      sysUserList(params).then((res) => {
        if (res.code == 200) {
          this.userlist = res.result.records;
        }
      });
    },
    searchChange(e) {
      this.syslist(e);
    },
    add() {
      //初始化默认值
      this.edit({});
      const obj = {
        pushType:'咨询消息'
      }
      this.model = Object.assign({}, obj);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    close() {
      this.$emit("close");
      this.visible = false;
      this.$refs.form.clearValidate();
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          this.model["sendid"] = this.sendid;
          this.model["pushMethod"] = "1";
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
          } else {
            httpurl += this.url.edit;
            method = "put";
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.close();
    },
  },
};
</script>

<style lang="less" scoped></style>
